Now this documentation is specific to MongoDB, which means it's
going to work with all of the drivers. In this case, it is going to work
with our Node.js driver. If we scroll down further, we can look at all
of the update operators we have access to. The most important, and
the one we're going to get started with, is the $set operator. This lets
us set a field's value inside of our update, which is exactly what we
want to do. There's other operators, like increment. This one, $inc,
lets you increment a field's value, like the age field in our Users
collection. Although these are super useful, we're going to get
started with $set. In order to use one of these operators, what we
need to do is type it out, $set, and then set it equal to an object. In
this object, these are the things that we're actually going to be
setting. For example, we want to set completed equal to true. If we tried
to put completed equal to true at the root of the object like this, it would
not work as expected. We have to use these update operators, which
means we need this. Now that we have our updates in place using
the set update operator, we can go ahead and provide our third and
final argument. If you head over to the documentation for
findOneAndUpdate, we can take a look at the options real quick. The one we
care about is returnOriginal.
The returnOriginal method is defaulted to true, which means that it
returns the original document, not the updated one, and we don't
want that. When we update a document, we want to get back that
updated document. What we're going to do is set returnOriginal to
false, and that's going to happen in our third and final argument.
This one is also going to be an object, returnOriginal, which is going to
be setting equal to false.
With this in place, we are done. We can tack on a then call to do
something with the results. I'll get my result back and I can simply
print it to the screen, and we can take a look at exactly what comes
back: